package com.binest.register.controller;

import com.binest.common.core.controller.BaseController;
import com.binest.common.core.domain.AjaxResult;
import com.binest.register.service.IMailService;
import com.binest.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import com.binest.common.core.redis.RedisCache;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/register")
public class RegisterController extends BaseController {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IMailService iMailService;


    /**
     * 获取验证码
     * @param email
     * @return
     * @throws Exception
     */
    @GetMapping("/captcha")
    public AjaxResult captcha( String email) throws Exception {


        Map<String, Object> data = new HashMap<>();
        //验证码  6位数字
        String verifyCode = RandomUtil.runNumber();
        String smsContent = "验证码：" + verifyCode + "，如非本人操作，请忽略。";
        String  code= redisCache.getCacheObject(email);
        if(code==null){
            redisCache.setCacheObject(email, verifyCode, 300, TimeUnit.SECONDS);
        }else if(!verifyCode.equals(code)){
            redisCache.deleteObject(email);
            redisCache.setCacheObject(email, verifyCode, 300, TimeUnit.SECONDS);
        }
        //发送邮箱
        iMailService.sendSimpleMail(email,"测试验证码",smsContent);



        //向前台返回
//        data.put("captchaCode", verifyCode);
        return AjaxResult.success("发送成功");
    }




}
